package com.gsafety.wellsfort.service.organization.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gsafety.wellsfort.common.context.LoginContext;
import com.gsafety.wellsfort.common.context.LoginContextUtil;
import com.gsafety.wellsfort.common.enums.StatusEnum;
import com.gsafety.wellsfort.common.util.ApiResponse;
import com.gsafety.wellsfort.common.util.ResultCode;
import com.gsafety.wellsfort.common.util.StringUtil;
import com.gsafety.wellsfort.dao.organization.ClassDAO;
import com.gsafety.wellsfort.dao.organization.SchoolDAO;
import com.gsafety.wellsfort.dao.organization.StudentDAO;
import com.gsafety.wellsfort.domain.dto.school.ClassDTO;
import com.gsafety.wellsfort.domain.model.organization.Class;
import com.gsafety.wellsfort.domain.model.organization.School;
import com.gsafety.wellsfort.domain.model.organization.Student;
import com.gsafety.wellsfort.domain.model.security.Admin;
import com.gsafety.wellsfort.domain.vo.ClassVO;
import com.gsafety.wellsfort.service.organization.IClassService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @Author: wuming
 * @Date: 2020/5/2 下午3:42
 * @Description
 */
@Service
@Slf4j
public class ClassServiceImpl implements IClassService {

    @Autowired
    private StudentDAO studentDAO;
    @Autowired
    private ClassDAO classDAO;

    @Autowired
    private SchoolDAO schoolDAO;


    /**
     * 删除课程
     *
     * @param id
     * @return
     */
    @Override
    public ApiResponse deleteClass(Integer id) {
        //先判断该班级下面有没有学生,有学生不允许删除
        List<Student> list = studentDAO.selectByClassId(id);
        if (list != null && list.size() > 0) {
            return ApiResponse.error(ResultCode.CLASS_CONTAINS_STUDENT);
        }
        int update = classDAO.deleteByPrimaryKey(id);
        return update > 0 ? ApiResponse.success() : ApiResponse.error();
    }

    /**
     * 获取所有课程
     *
     * @param classDTO
     * @return
     */
    @Override
    public ApiResponse getAllClass(ClassDTO classDTO) {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String requestDate = dateFormat.format(new Date());
        Admin admin = LoginContextUtil.getAdmin();
        if (admin != null && admin.getSchoolId() != 1) {
            classDTO.setSchoolId(admin.getSchoolId());
        }
        Page page = PageHelper.startPage(classDTO.getPageNo(), classDTO.getPageSize());
        List<Class> classList = classDAO.selectByParam(classDTO);
        PageInfo pageInfo = new PageInfo(page);
        if (classList != null && classList.size() > 0) {
            for (Class cls : classList) {
                School school = schoolDAO.selectByPrimaryKey(cls.getSchoolId());
                cls.setSchoolName(school.getName());
            }
        }
        pageInfo.setList(classList);
        String responseDate = dateFormat.format(new Date());
        return ApiResponse.success(classList, (int) pageInfo.getTotal(), "请求时间:" + requestDate + "   响应时间:" + responseDate);
    }

    /**
     * 修改班级信息
     *
     * @param record
     * @return
     */
    @Override
    public ApiResponse updateClassInfo(Class record) {
        record.setModified(new Date());
        record.setOperator(LoginContextUtil.getUserName());
        return classDAO.updateByPrimaryKeySelective(record) == 1 ?
                ApiResponse.success() : ApiResponse.error();
    }

    @Override
    public ApiResponse addClass(ClassVO classVO) {
        Class record = new Class();
        Date date = new Date();
        record.setCreated(date);
        record.setModified(date);
        record.setCreator(LoginContextUtil.getUserName());
        record.setOperator(LoginContextUtil.getUserName());
        record.setStatus(classVO.getStatus() == null ? StatusEnum.ENABLE.getValue().byteValue() : classVO.getStatus().byteValue());
        BeanUtils.copyProperties(classVO, record);
        if (classDAO.insertSelective(record) > 0) {
            return ApiResponse.success();
        }
        return ApiResponse.error();
    }

    @Override
    public ApiResponse batchAddClass(List<ClassVO> list, Integer schoolId) {
        if (list != null && list.size() > 0) {
            for (ClassVO classVO : list) {
                if (StringUtils.isEmpty(classVO.getName())) {
                    return ApiResponse.error(ResultCode.ERROR, "班级名称不能为空!");
                }
                if (StringUtils.isEmpty(classVO.getManagerName())) {
                    return ApiResponse.error(ResultCode.ERROR, "负责人姓名不能为空!");
                }
                if (StringUtils.isEmpty(classVO.getManagerPhone())) {
                    return ApiResponse.error(ResultCode.ERROR, "负责人电话不能为空!");
                }
                if (classVO.getStatus() == null || (classVO.getStatus().intValue() != 0 && classVO.getStatus().intValue() != 1)) {
                    return ApiResponse.error(ResultCode.ERROR, "状态输入不合法!");
                }
                classVO.setSchoolId(schoolId);
                this.addClass(classVO);
            }
        }
        return ApiResponse.success();
    }

    @Override
    public Integer count() {
        return classDAO.count();
    }
}
